package org.particlethink.mapper.impl;

import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.particlethink.exception.TokenException;
import org.particlethink.utils.ObjectUtils;
import org.particlethink.utils.PathUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ProjectRequestHelper {
    private static final String projectUrl = "/material/api/projects";
    private final static ObjectMapper objectMapper = new ObjectMapper();
    private static final Logger LOGGER = LoggerFactory.getLogger(ProjectRequestHelper.class);


    public static List<Data> queryProjects(String url, String name, String managementLevel, String token) throws URISyntaxException, IOException {

        List<Data> result;
        Map<String, Object> params = new HashMap<>();

        if (name != null && !name.isEmpty()) {
            params.put("name", name);
        }

        if (managementLevel != null && !managementLevel.isEmpty()) {
            params.put("managementLevel", managementLevel);
        }

        try (HttpResponse response = HttpUtil.createPost(url + projectUrl).body(objectMapper.writeValueAsString(params), "application/json").header("Authorization", "Bearer " + token).execute()) {
            LOGGER.debug("queryProjects resp:{},{},{}", url + projectUrl, objectMapper.writeValueAsString(params), new String(response.bodyBytes()));
            Response resp = objectMapper.readValue(response.bodyBytes(), Response.class);
            if (resp.getData() == null) {
                result = new ArrayList<>();
            } else {
                if (resp.code == 401) {
                    throw new TokenException();
                }
                result = new ArrayList<>(resp.getData());
            }
        }

        return result;
    }

    public static class Request {
        private String name;
        private String managementLevel;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getManagementLevel() {
            return managementLevel;
        }

        public void setManagementLevel(String managementLevel) {
            this.managementLevel = managementLevel;
        }
    }

    private static class Response {
        private String msg;
        private Integer code;
        private List<Data> data;

        public String getMsg() {
            return msg;
        }

        public void setMsg(String msg) {
            this.msg = msg;
        }

        public Integer getCode() {
            return code;
        }

        public void setCode(Integer code) {
            this.code = code;
        }

        public List<Data> getData() {
            return data;
        }

        public void setData(List<Data> data) {
            this.data = data;
        }
    }

    public static class Data {
        private Integer id;
        private String resourceId;
        private String name;
        private String projectCode;
        private String province;
        private String city;
        private String address;
        private String managementLevel;
        private String remarks;

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public String getResourceId() {
            return resourceId;
        }

        public void setResourceId(String resourceId) {
            this.resourceId = resourceId;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getProjectCode() {
            return projectCode;
        }

        public void setProjectCode(String projectCode) {
            this.projectCode = projectCode;
        }

        public String getProvince() {
            return province;
        }

        public void setProvince(String province) {
            this.province = province;
        }

        public String getCity() {
            return city;
        }

        public void setCity(String city) {
            this.city = city;
        }

        public String getAddress() {
            return address;
        }

        public void setAddress(String address) {
            this.address = address;
        }

        public String getManagementLevel() {
            return managementLevel;
        }

        public void setManagementLevel(String managementLevel) {
            this.managementLevel = managementLevel;
        }

        public String getRemarks() {
            return remarks;
        }

        public void setRemarks(String remarks) {
            this.remarks = remarks;
        }
    }
}
